/**
 * 自定义函数
 * */

var adminLogin; //登录信息
var routes = {
    login: '/login.html',   //登录路由
    index: '/index.html',   //首页
    scenceIndex: '/views/scence.html',  //场景
    videoIndex: '/views/video.html',  //视频
    qrIndex: '/views/qr.html',  //二维码
    qrLogIndex: '/views/qrlog.html',  //扫码
    userIndex: '/views/user.html',  //用户
    shopIndex: '/views/shop.html',  //门店
    goods1Index: '/views/tmps/shop1Goods.html',  //商品
    tmp1Index: '/views/tmps/tmp1.html',  //模板1
    tmp1Comment: '/views/tmps/tmp1Comment.html',  //模板1评论
    tmp1History: '/views/tmps/tmp1History.html',  //模板1历史
    adPlace: '/views/ad/place.html',  //广告位
    adShop: '/views/ad/shop.html',  //广告商家推广
    adRank: '/views/ad/rank.html',  //广告排队展示
    adStat: '/views/ad/stat.html'  //广告已展示
}; //前端页面路由地址
var apis = {
    login: {    //登录
        method: 'post',
        url: '/admin/login'
    },
    scenceIndex: {  //场景列表
        method: 'get',
        url: '/admin/scence/index'
    },
    scenceAdd: {  //场景新增
        method: 'post',
        url: '/admin/scence/add'
    },
    scenceModify: {  //场景修改
        method: 'post',
        url: '/admin/scence/modify'
    },
    scenceDel: {  //场景删除
        method: 'post',
        url: '/admin/scence/del'
    },
    videoIndex: {   //视频列表
        method: 'get',
        url: '/admin/video/index'
    },
    videoAdd: {   //视频新增
        method: 'post',
        url: '/admin/video/add'
    },
    videoModify: {   //视频修改
        method: 'post',
        url: '/admin/video/modify'
    },
    videoDel: {   //视频删除
        method: 'post',
        url: '/admin/video/del'
    },
    qrIndex: {   //码列表
        method: 'get',
        url: '/admin/qr/index'
    },
    qrAdd: {   //码新增
        method: 'post',
        url: '/admin/qr/add'
    },
    qrLogIndex: {   //扫码列表
        method: 'get',
        url: '/admin/qrlog/index'
    },
    visitorIndex: {   //游客列表
        method: 'get',
        url: '/admin/visitor/index'
    },
    userIndex: {   //用户列表
        method: 'get',
        url: '/admin/user/index'
    },
    weixinIndex: {   //微信用户列表
        method: 'get',
        url: '/admin/weixin/index'
    },
    tmp1Index: {   //模板1列表
        method: 'get',
        url: '/admin/tmp1/index'
    },
    tmp1Add: {   //模板1新增
        method: 'post',
        url: '/admin/tmp1/add'
    },
    tmp1Modify: {   //模板1修改
        method: 'post',
        url: '/admin/tmp1/modify'
    },
    tmp1Follow: {   //模板1关注
        method: 'get',
        url: '/admin/tmp1/follow/index'
    },
    tmp1Comment: {   //模板1评论
        method: 'get',
        url: '/admin/tmp1/comment/index'
    },
    tmp1History: {   //模板1历史
        method: 'get',
        url: '/admin/tmp1/history/index'
    },
    shopIndex: {   //门店列表
        method: 'get',
        url: '/admin/shop/index'
    },
    shopAdd: {   //门店新增
        method: 'post',
        url: '/admin/shop/add'
    },
    shopModify: {   //门店修改
        method: 'post',
        url: '/admin/shop/modify'
    },
    shop1CateIndex: {   //门店分类列表
        method: 'get',
        url: '/admin/shop1/cate/index'
    },
    shop1SelIndex: {   //门店选项
        method: 'get',
        url: '/admin/shop1/sel'
    },
    shop1OrderIndex: {   //门店订单列表
        method: 'get',
        url: '/admin/shop1/order/index'
    },
    shop1FollowIndex: {   //门店关注列表
        method: 'get',
        url: '/admin/shop1/follow/index'
    },
    shop1CommentIndex: {   //门店评论列表
        method: 'get',
        url: '/admin/shop1/comment/index'
    },
    goods1Index: {   //商品列表
        method: 'get',
        url: '/admin/goods1/index'
    },
    goods1Add: {   //商品新增
        method: 'post',
        url: '/admin/goods1/add'
    },
    goods1Modify: {   //商品修改
        method: 'post',
        url: '/admin/goods1/modify'
    },
    adPlaceIndex: {   //广告位列表
        method: 'get',
        url: '/admin/ad/place/index'
    },
    adShopIndex: {   //广告商家列表
        method: 'get',
        url: '/admin/ad/shop/index'
    },
    adShopSel: {   //广告商家选项
        method: 'get',
        url: '/admin/ad/shop/sel'
    },
    adShopAdd: {   //广告商家新增
        method: 'post',
        url: '/admin/ad/shop/add'
    },
    //adShopModify: {   //广告商家修改
    //    method: 'post',
    //    url: '/admin/ad/shop/modify'
    //},
    //adShopStatus: {   //广告商家状态
    //    method: 'post',
    //    url: '/admin/ad/shop/status'
    //},
    adShopRun: {   //广告启用
        method: 'post',
        url: '/admin/ad/shop/run'
    },
    adShopDel: {   //广告删除
        method: 'post',
        url: '/admin/ad/shop/del'
    },
    //adShopRankIndex: {   //广告商家排队
    //    method: 'get',
    //    url: '/admin/ad/rank/index'
    //},
    adShopStatIndex: {   //广告商家已展示
        method: 'get',
        url: '/admin/ad/stat/index'
    }
}; //后端接口地址


//显示字符串前几个字符
function strPrefix(str, n){
    if (str.length <= n) {
        return str;
    } else {
        return str.substring(0, n) + '...';
    }
}


//抓图
function uploadImage() {
    var img = $("#viewimg"), dataURL, fileObj = $('#uploadFile')[0],
        windowURL = window.URL || window.webkitURL, reader = new FileReader();
    dataURL = windowURL.createObjectURL(fileObj.files[0]); //要显示图片的路径
    img.attr("src", dataURL); //显示图片
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (e){
        //console.log(e.target.result);
        //$('#img')[0].value = e.target.result; //这一步是关键
        showImg(e.target.result);
    }
}


//登录aid
function getAid(){
    var adminInfo = getAdminInfo();
    if (!adminInfo) {
        msgPop({msg: '没有登录'}); return
    }
    return adminInfo.aid + '<>' + adminInfo.adminName;
}


// 分离header头部
function getHeader(){
    // <div class="navbar navbar-fixed-top"></div>
    //     <div class="container-fluid cl" style="background: #222;"></div>
    //         <a class="logo navbar-logo" href="javascript:;" style="color: #fff;">管理后台</a>
    //         <nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs"></nav>
    //             <ul class="cl">
    //                 <li class="dropDown dropDown_hover">
    //                     <a href="javascript:;" class="dropDown_A" style="color: #fff;"></a>
    //                         admin <i class="Hui-iconfont">&#xe6d5;</i>
    //                     </a>
    //                     <ul class="dropDown-menu menu radius box-shadow">
    //                         <li><a href="#">退出</a></li>
    //                     </ul>
    //                 </li>
    //             </ul>
    //         </nav>
    //     </div>
    // </div>
    var h = '';
    h += '<div class="navbar navbar-fixed-top">';
    h += '<div class="container-fluid cl" style="background: #222;">';
    h += '<a class="logo navbar-logo" href="javascript:;" style="color: #fff;">系统后台</a>';
    h += '<nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs">';
    h += '<ul class="cl">';
    h += '<li class="dropDown dropDown_hover">';
    h += '<a href="javascript:;" class="dropDown_A" style="color: #fff;">';
    h += 'admin';
    h += ' <i class="Hui-iconfont">&#xe6d5;</i>';
    h += '</a>';
    h += '<ul class="dropDown-menu menu radius box-shadow">';
    h += '<li><a href="#">退出</a></li>';
    h += '</ul>';
    h += '</li>';
    h += '</ul>';
    h += '</nav>';
    h += '</div>';
    h += '</div>';
    $('#navHeader').html(h);
}


// 分离左侧menu
function getLeftMenu(){
    // <div class="menu_dropdown bk_2">
    //     <dl id="menu-article">
    //         <dt>
    //             <i class="Hui-iconfont">&#xe616;</i> 
    //             资讯管理
    //             <i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i>
    //         </dt>
    //         <dd>
    //             <ul>
    //                 <li>
    //                     <a href="article-list.html" title="资讯管理">资讯管理</a>
    //                 </li>
    //             </ul>
    //         </dd>
    //     </dl>
    //     <dl id="menu-member">
    //         <dt>
    //             <i class="Hui-iconfont">&#xe60d;</i> 
    //             会员管理
    //             <i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i>
    //         </dt>
    //         <dd>
    //             <ul>
    //                 <li>
    //                     <a href="member-list.html" title="会员列表">会员列表</a>
    //                 </li>
    //             </ul>
    //         </dd>
    //     </dl>
    // </div>
    var ms = [
        {
            id: 1,
            title: '码源管理',
            childs: [
                { id: 11, title: '应用场景', link: 'ewms/scence.html' },
                { id: 12, title: '引导视频', link: 'ewms/video.html' },
                { id: 13, title: '二维码', link: 'ewms/qr.html' },
                { id: 14, title: '扫码统计', link: 'ewms/qrlog.html' }
            ]
        },
        {
            id: 2,
            title: '用户管理',
            childs: [
                { id: 21, title: '游客用户', link: 'users/visitor.html' },
                { id: 22, title: '个人用户', link: 'users/user.html' },
                { id: 23, title: '微信用户', link: 'users/weixin.html' },
                { id: 24, title: '门店管理', link: 'users/shop.html' }
                //{ id: 22, title: 'APP用户', link: '' },
                //{ id: 23, title: 'EXE用户', link: '' },
                //{ id: 24, title: '个人用户', link: '' }
                //{ id: 25, title: '商家用户', link: '' }
                //{ title: '企业用户', link: '' }
            ]
        },
        {
            id: 3,
            title: '业务管理',
            childs: [
                { id: 31, title: '个人模板1', link: 'tmps/tmp1.html' },
                //{ id: 32, title: '模板1评论', link: 'tmps/tmp1Comment.html' },
                //{ id: 33, title: '模板1历史', link: 'tmps/tmp1History.html' },
                { id: 34, title: '商家模板1', link: 'tmps/shop1.html' }
            ]
        },
        {
            id: 4,
            title: '广告管理',
            childs: [
                //{ id: 41, title: '广告位', link: 'ads/place.html' },
                ////商家添加广告作宣传推广
                { id: 42, title: '广告投放', link: 'ads/shop.html' },
                ////付费展示已添加的广告
                //{ id: 43, title: '商家排队', link: 'ads/rank.html' },
                //已经展示过的广告
                { id: 44, title: '已展示', link: 'ads/stat.html' }
                //{ title: '产品推荐', link: '' }
            ]
        }
        //{
        //    id: 5,
        //    title: '收支管理',
        //    childs: [
        //        { id: 51, title: '平台收入', link: 'income.html' },
        //        { id: 52, title: '广告付费', link: 'adFee.html' },
        //        { id: 53, title: '商家收入', link: 'adIncome.html' }
        //    ]
        //}
    ];
    var h = '', i, ii, childs;
    h += '<div class="menu_dropdown bk_2">';
    h += '<a href="index.html">';
    //h += '<dl>';
    h += '<dt><i class="Hui-iconfont"></i> 首页统计</dt>';
    //h += '</dl>';
    h += '</a>';
    for (i = 0; i < ms.length; i ++) {
        h += '<dl id="menu-article">';
        //h += '<dt>';
        h += '<dt class="selected">';
        h += '<i class="Hui-iconfont">&#xe616;</i> ';
        h += ms[i].title;
        h += '<i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i>';
        h += '</dt>';
        //h += '<dd>';
        h += '<dd style="display: block;">';
        h += '<ul>';
        childs = ms[i].childs;
        for (ii = 0; ii < childs.length; ii ++) {
            h += '<li>';
            h += '<a href="javascript:;" onclick="toLeftMenu(' + childs[ii].id + ');">';
            h += childs[ii].title;
            h += '<span style="margin-right: 10px; float: right;" class="lma" id="lma-' + childs[ii].id + '"></span>';
            h += '</a>';
            h += '<input type="hidden" id="mlink-' + childs[ii].id + '" value="/views/' + childs[ii].link + '"/>';
            h += '</li>';
        }
        h += '</ul>';
        h += '</dd>';
        h += '</dl>';
    }
    h += '</div>';
    $('#leftMenu').html(h);
}


//切换菜单
function toLeftMenu(v){
    var link = $('#mlink-' + v).val();
    console.log(link);
    if (link == '') {
        msgPop({msg: '没有地址'}); return
    }
    $('.lma').html('');
    $('#lma-' + v).html('→');
    $('iframe').attr('src', link);
}
//切换首页
function toHome(){
    $('.lma').html('');
    $('iframe').attr('src', '/index.html');
}


//获取当前年月日
function currYmd(){
    var currDate = new Date();
    var currYear = currDate.getFullYear().toString();
    var currMonth = currDate.getMonth().toString();
    var currDay = currDate.getDate().toString();
    if (currMonth.length < 2) {
        currMonth = '0' + currMonth;
    }
    if (currDay.length < 2) {
        currDay = '0' + currDay;
    }
    var ymd =  currYear+ '' + currMonth + '' + currDay;
    return ymd * 1
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');},2000)</script>';
    $('#msgPop').html(h);
}


//获取登录信息
function getAdminInfo(){
    return getCache(adminLogin);
}


//设置登录信息
function setAdminInfo(e){
    return setCache({key: adminLogin, val: e.val});
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除指定缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//清理缓存
function clearCache(){
    window.localStorage.clear();
}